<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\api\controller\Cloudcomputing;
use app\common\model\WithdrawalChannel;
use think\Db;

/**
 * 银行卡
 */
class Bankcard extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';
    /**
     * 银行卡列表
     */
    public function lists()
    {
        if ($this->auth->id) {
            $where = [
                "sale_id" => $this->auth->id,
                "status" => 1
            ];
            $count = Db::name("bank_card")->where($where)->count();
            $data = Db::name("bank_card")->where($where)->order("create_time desc")->select();
            $this->success("请求成功", ["count" => $count, "data" => $data]);
        }
        $this->error("请求失败！");
    }

    /**
     * 获取银行卡信息
     */
    public function getBankCardDetail()
    {
        if ($this->auth->id) {
            $where = [
                "sale_id" => $this->auth->id,
                "status" => 1
            ];
            $data = Db::name("bank_card")->where($where)->order("create_time desc")->find();
            $this->success("请求成功", $data);
        }
        $this->error("请求失败！");
    }

    /**
     * 保存银行卡信息
     */
    public function bankCardSave()
    {
        $this->request->filter(['strip_tags', 'trim']);
        $bank_card_id = $this->request->post('bank_card_id');
        if ($this->auth->id) {
            $data = [
                "name" => $this->request->post('name'),
                "phone" => $this->request->post('phone'),
                "idcards" => $this->request->post('idcards'),
                "bank_card_number" => $this->request->post('bank_card_number')
            ];
            $portData = bankCardVerifi($data);
            if ($portData["code"] == 20000) {
                $data["sale_id"] = $this->auth->id;
                $data["card_type"] = $portData["data"]["card_type"];
                $data["card_name"] = $portData["data"]["card_name"];
                $data["bank_name"] = $portData["data"]["bank_name"];
                $data["bank_logo"] = $portData["data"]["bank_logo"];
                $data["bank_id"] = $portData["data"]["bank_id"];
                $old_data = Db::name("bank_card")->where('sale_id', $this->auth->id)->where('status', '<>', 3)->count();
                if ($old_data > 0) {
                    $up = Db::name("bank_card")->where('sale_id', $this->auth->id)->update(["status" => 3]);
                    if ($up) {
                        $data["create_time"] = time();
                        $res = Db::name("bank_card")->insert($data);
                    }
                } else {
                    $data["create_time"] = time();
                    $res = Db::name("bank_card")->insert($data);
                    // 从业者数据提交
                    $withdrawal_channel = WithdrawalChannel::where('status', 1)->find();
                    if (!empty($withdrawal_channel)) {
                        $cloudcomputingData = [
                            "enterprise_code" => $withdrawal_channel->enterprise_code,
                            "channel_code" => $withdrawal_channel->channel_code,
                            "task_code" => $withdrawal_channel->task_code,
                            "public_key" => $withdrawal_channel->public_key
                        ];
                        Cloudcomputing::practitionerDataSubmission(["sale_id" => $this->auth->id], $cloudcomputingData);
                    }
                }
                if ($res) {
                    $this->success("保存成功");
                } else {
                    $this->error("保存失败，请重新绑定银行卡");
                }
            } else {
                $this->error($portData["msg"]);
            }
        }
        $this->error("请求失败！");
    }

    public function test()
    {
        Cloudcomputing::practitionerDataSubmission(["sale_id" => $this->auth->id]);
    }
}
